package Lee_code;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @author blh
 * @date 2024年07月31日 15:35
 */
public class Lee_71_731 {
    public String simplifyPath(String path) {
        Deque<String> deque = new ArrayDeque<>();
        int index = 0;
        while (index < path.length()) {
            if (path.charAt(index) == '/') {
                // deque.push("/");
                while (index < path.length() && path.charAt(index) == '/') {
                    index++;
                }
            } else {
                StringBuilder temp = new StringBuilder();
                while (index < path.length() && path.charAt(index) != '/' ) {
                    temp.append(path.charAt(index));
                    index++;
                }
                String tempStr = temp.toString();
                if (".".equals(tempStr)){
                    continue;
                } else if ("..".equals(tempStr)) {
                    deque.pollLast();
                } else {
                    deque.offer(tempStr);
                }
            }
        }
        StringBuilder res = new StringBuilder();
        while (!deque.isEmpty()){
            res.append('/');
            res.append(deque.poll());
        }
        if (res.toString().isEmpty()){
            return "";
        }
        return res.toString();
    }
}
